using System.Collections.Generic;
using MyMovieCollection.DATA.DataAccess.Interfaces;
using MyMovieCollection.DATA.Models;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace MyMovieCollection.DATA.DataAccess.AR
{
    public class MovieDao : IMovieDao
    {
        public List<Movie> GetAll()
        {
            return new List<Movie>(ActiveRecordBase<Movie>.FindAll());
        }
        public Movie GetById(int id)
        {
            return ActiveRecordBase<Movie>.Find(id);
        }

        public void AddMovie(Movie movie)
        {
            if (movie != null)
                movie.CreateAndFlush();
        }
        public List<Movie> GetByMovieType(MovieType movieType)
        {
            return new List<Movie>( ActiveRecordBase<Movie>.FindAllByProperty("Movie_Type", movieType));
        }

        public List<Movie> GetByMoviePointScale(double minPoint, double maxPoint)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For(typeof (Movie));
            dCriteria.Add(Expression.Ge("Point", minPoint));
            dCriteria.Add(Expression.Le("Point", maxPoint));
            return new List<Movie>(ActiveRecordBase<Movie>.FindAll(dCriteria));
        }

        public List<Movie> GetAllByCategoryAndPointScale(PointScale pointScale,MovieType category)
        {
            var dCriteria = DetachedCriteria.For(typeof(Movie));
            dCriteria.Add(Expression.Ge("Point", pointScale.MinPoint));
            dCriteria.Add(Expression.Le("Point", pointScale.MaxPoint));
            dCriteria.Add(Expression.Eq("Movie_Type", category));
            return new List<Movie>(ActiveRecordBase<Movie>.FindAll(dCriteria));
        }
        public List<Movie> GetAllByCriterias(string Name, int Year, PointScale pointScale, MovieType category)
        {

            var dCriteria = DetachedCriteria.For(typeof (Movie));
            if (pointScale != null)
            {

                dCriteria.Add(Expression.Ge("Point", pointScale.MinPoint));
                dCriteria.Add(Expression.Le("Point", pointScale.MaxPoint));
            }
            if (Name != "")
            {
                dCriteria.Add(Expression.Like("Name", Name, MatchMode.Anywhere));
            }
            if (Year != 0)
            {
                dCriteria.Add(Expression.Eq("Year", Year));
            }
            if (category != null)
            {
                dCriteria.Add(Expression.Eq("Movie_Type", category));
            }
            return new List<Movie>(ActiveRecordBase<Movie>.FindAll(dCriteria));
        }

        public void DeleteMovie(int movieID)
        {
            Movie movie =Movie.Find(movieID);
            movie.Delete();
        }
    }
}